package com.atguigu.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 自定义组件 —— 语言视图解析器
 *
 * @author sx
 * @date 2022-02-21 10:34
 */
public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lang = request.getParameter("lang");
        System.out.println("=======>lang:"+lang);

        //如果没有就使用默认的
        Locale locale = Locale.getDefault();

        if (!StringUtils.isEmpty(lang)) {
            String[] split = lang.split("-");
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
